﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;
using System.IO;

namespace Comm
{
    public class Help
    {
        private static string noimage = ConfigurationManager.AppSettings["NoImage"].ToString();//无法显示图片是的替代图片
        /// <summary>
        /// 显示图片  (过滤了图片无法显示的情况)
        /// </summary>
        /// <param name="src">图片路径</param>
        /// <returns>显示图片</returns>
        public static string checkImage(object path,string name)
        {
            if (path == null || path.ToString().Trim() == "")
            {
                if (string.IsNullOrEmpty(name))
                {
                    return noimage;
                }
                else
                {
                    return System.Text.RegularExpressions.Regex.Replace(noimage, @"\.(.*)$", "") + name + Path.GetExtension(noimage);
                }
            }
            string src = path.ToString().Trim();
            if (!System.Text.RegularExpressions.Regex.IsMatch(src, "^http", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
            {
                if (!System.IO.File.Exists(HttpContext.Current.Server.MapPath(src)))
                {
                    if (string.IsNullOrEmpty(name))
                    {
                        return noimage;
                    }
                    else
                    {
                        return System.Text.RegularExpressions.Regex.Replace(noimage, @"\.(.*)$", "") + name + Path.GetExtension(noimage);
                    }
                }
            }

            return src;
        }

        /// <summary>
        /// 显示图片  (过滤了图片无法显示的情况)
        /// </summary>
        /// <param name="src">图片路径</param>
        /// <returns>显示图片</returns>
        public static string checkImage(object path)
        {
            return checkImage(path, null);
        }

        /// <summary>
        /// 获取经过系统关键词处理的内容
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string getContentBysystenkeyword(object obj)
        {
            string content = "";
            if (obj == null || obj.ToString().Trim() == "")
            {
                return "";
            }
            content = obj.ToString();

            List<MSystemKeyword> list = HttpContext.Current.Cache["SystemKeyword"] as List<MSystemKeyword>;
            if (list == null)
            {
                BSystemKeyword syskeywordbll = new BSystemKeyword();
                syskeywordbll.path = HttpContext.Current.Server.MapPath("/App_Data/SystemKeywords.xml");
                list = syskeywordbll.getList("Enabled=True");

                System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(
                        HttpContext.Current.Server.MapPath("/App_Data/SystemKeywords.xml"));
                HttpContext.Current.Cache.Insert("SystemKeyword", list, dep);
            }

            foreach (MSystemKeyword m in list)
            {
                string target = " target=\"_blank\"";
                content = System.Text.RegularExpressions.Regex.Replace(content, "(?<=[^>]*)(" + m.LookExpression + ")(?![^<]*</a>)", "<a title=\"" + m.Title + "\""
                    + "alt=\"" + m.Alt + "\"" + target + " href=\"" + m.Link + "\">$1</a>");
            }

            return content;
        }

        /// <summary>
        /// 获取伪静态文件后缀
        /// </summary>
        /// <returns></returns>
        public static string getHtmlSuffix()
        {
            return ".htm";
        }

        /// <summary>
        /// 获取静态文件名称（不包括后缀）
        /// </summary>
        /// <returns></returns>
        public static string getHtmlName(string HtmlName)
        {
            return System.Text.RegularExpressions.Regex.Replace(HtmlName, @"\.html|\.htm", "");
        }

        /// <summary>
        /// 获取订单号
        /// </summary>
        /// <returns></returns>
        public static string getOrderNum()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssffff") + new Random().Next(1000000, 9999999);
        }

        /// <summary>
        /// 获取缩略图路径
        /// </summary>
        /// <param name="Thum"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string getThumbnailPic(string Thum, object path)
        {
            if (path == null || path.ToString().Trim() == "")
            {
                return "";
            }
            if (path.ToString().Contains("/"))
            {
                return path.ToString().Insert(path.ToString().LastIndexOf('/') + 1, Thum + "/");
            }

            return Thum + "/" + path.ToString();
        }

        /// <summary>
        /// 获取时间
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string getDateTime(object obj, string formate)
        {
            if (obj == null)
            {
                return "";
            }

            return Convert.ToDateTime(obj).ToString(formate);
        }

        /// <summary>
        /// 保存地址到Cookie中
        /// </summary>
        /// <param name="model"></param>
        public static void saveAddress(Cookie_Address model)
        {
            CookiesHelper.SetCookie("shopaddress", JavaScriptConvert.SerializeObject(model));
        }

        /// <summary>
        /// 获取Cookie中的地址
        /// </summary>
        /// <param name="model"></param>
        public static Cookie_Address getAddress()
        {
            string value = CookiesHelper.GetCookieValue("shopaddress");
            if (value.Trim() == "")
            {
                return null;
            }

            Cookie_Address model = (Cookie_Address)JavaScriptConvert.DeserializeObject(value, typeof(Cookie_Address));

            return model;
        }
    }

    /// <summary>
    /// 地址保存Cookie
    /// </summary>
    public class Cookie_Address
    {
        private string _consignee_name;
        private long _area;
        private string _address;
        private string _home_phone;
        private string _mobile_phone;
        private string _zipcode;
        private string _email;
        private int _paytypeid;
        private int _distributiontypeid;

        /// <summary>
        /// 
        /// </summary>
        public string Consignee_Name
        {
            set { _consignee_name = value; }
            get { return _consignee_name; }
        }
        /// <summary>
        /// 
        /// </summary>
        public long Area
        {
            set { _area = value; }
            get { return _area; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Address
        {
            set { _address = value; }
            get { return _address; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Home_Phone
        {
            set { _home_phone = value; }
            get { return _home_phone; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Mobile_Phone
        {
            set { _mobile_phone = value; }
            get { return _mobile_phone; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ZipCode
        {
            set { _zipcode = value; }
            get { return _zipcode; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Email
        {
            set { _email = value; }
            get { return _email; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int PaytypeID 
        {
            set { _paytypeid = value; }
            get { return _paytypeid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int DistributionTypeID
        {
            set { _distributiontypeid = value; }
            get { return _distributiontypeid; }
        }
    }
}
